package com.chuanyang.driverh.mainhome;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainhome.adapter.SystemMessageAdapter;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.eventbus.MessageData;
import com.chuanyang.driverh.utils.eventbus.MessageResult;
import com.chuanyang.driverh.utils.eventbus.RefleshMessageEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;
import okhttp3.Call;

/**
 * 西风多少恨，吹不散眉弯！—唐大帅
 *
 * @author 唐帅
 *         系统消息界面
 *         created at
 */

public class SystemMessageActivity extends BaseTitleActivity implements AdapterView.OnItemClickListener, SpringView.OnFreshListener {
    protected int mCurrentPage = 1;
    private SpringView springView;
    private ListView mlistview;
    private Intent intent;
    private String messagelist;
    private HashMap params;
    private int type;
    private SystemMessageAdapter adapter;
    @Override
    protected int getContentResId() {
        return R.layout.common_act_systemmessage;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setBackImg();
        intent = getIntent();
        //1系统消息 3实时消息
        type = intent.getIntExtra("type", 1);
//        messagelist = intent.getStringExtra("data");
        if (type == 1) {
            setTitle("系统消息");
        } else {
            setTitle("实时消息");//消息类型(1.系统消息 2.路况 3.实时 4.预警)

        }
        //找到基类中需要用到的控件
        springView = (SpringView) findViewById(R.id.springview);
        mlistview = (ListView) findViewById(R.id.lv_list);
        mlistview.setOnItemClickListener(this);
        springView.setListener(this);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));

    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);//如果不调用此方法，intent还会是之前的第一个intent,调用此方法后可以理解为activity重建
    }

    @Override
    public void initData() {
        List<MessageData.MesageDataBean> list = new ArrayList();
        adapter = new SystemMessageAdapter(this, list);
        mlistview.setAdapter(adapter);
        requsetData();

    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onRefreshData() {
        mCurrentPage = 1;
        requsetData();

    }

    @Override
    public void onLoadmoreData() {
        requsetData();

    }
    private void requsetData() {
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("userId", SharedPrefUtil.getUserId());
//        params.put("userId", "829");
        params.put("sendType", type + "");
        params.put("length", "10");
        params.put("page", mCurrentPage + "");
        MyHttpUtil.selectMessageList(activityContext,params, new MyCallback<MessageResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(MessageResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {

                    if (result.getData() != null && result.getData().getData().size()> 0)
                    {
                        //adapter.setData(result.getData().getData());
                        setDataResult(result.getData().getData());
                       SharedPrefUtil.setPreferences( "messagecount"+ SharedPrefUtil.getUserId(), 0);
                        EventBus.getDefault().post(new RefleshMessageEvent(true,1));

                    }
                } else {
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }



    public void onEventMainThread(RefleshMessageEvent event) {
        if (event.isReflesh()&&event.getIndex()==0) {
            mCurrentPage=1;
            requsetData();
        }

    }
    /**
     * 设置数据源
     *
     * @param list
     */
    protected void setDataResult(List<MessageData.MesageDataBean> list) {
        if (mlistview == null) {
            return;
        }
        if (mCurrentPage == 1) {
            adapter.clear();
        }

        if (list != null && list.size() > 0)
        {
            adapter.addAll(list);
            mCurrentPage += 1;
//            getEmptyView().setVisibility(View.GONE);

        } else {
            if (adapter.getCount() > 0) {    //表示列表展示的有数据,且加载更多时没有更多
                // getEmptyView().setVisibility(View.GONE);
                PrintUtil.toast(this,   "数据已全部加载");
            } else {
//                getEmptyView().setVisibility(View.VISIBLE);

            }


        }
        springView.onFinishFreshAndLoad();


    }
}
